package com.peas.platform.module.icims.base.approve.domain.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.approve.adapter.repository.ApproveRepository;
import com.peas.platform.module.icims.base.approve.adapter.repository.po.ApprovePo;
import com.peas.platform.module.icims.base.approve.domain.ApproveDomain;
import com.peas.platform.module.icims.base.approve.dto.ApproveDto;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
public class ApproveDomainImpl extends BaseDomainImpl<ApproveDto, ApprovePo> implements ApproveDomain {

    @Resource
    private ApproveRepository approveRepository;
    @Resource
    private SequenceService sequenceService;

    @Override
    public BaseRepository<ApprovePo> getMapper() {
        return approveRepository;
    }

    @Override
    public Class<? extends BaseRepository<ApprovePo>> getRepositoryClass() {
        return ApproveRepository.class;
    }

    @Transactional
    @Override
    public int save(ApproveDto dto) {
        if (StrUtil.isBlank(dto.getApproveCode())) {
            String code = sequenceService.genSequence("base", "ApproveDto", "AD", 4, 1);
            dto.setApproveCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("approveCode", dto.getApproveCode());
        return result;
    }

    @Transactional
    @Override
    public int updateById(ApproveDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("approveCode", dto.getApproveCode());
        return result;
    }

}
